package cn.edu.mju.property.web.controller;

import cn.edu.mju.property.entity.User;
import cn.edu.mju.property.service.UserService;
import cn.edu.mju.property.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserList")
public class UserList extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

/*
        UserService service = new UserServiceImpl();
        List<User> users = null;
        try {
            users = service.findAll();
            request.setAttribute("users",users);
            request.getRequestDispatcher("/member-list.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
*/



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        List<User> users = null;
        try {
            users = service.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }


        response.setContentType("application/json; charset=UTF-8");
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        String jsonString = JSON.toJSONString(users);

        response.getWriter().write("{\"code\":0,\"msg\":null,\"count\":3,\"data\":" + jsonString+"}");

    }
}
